package cn.flashk.controls {
	import com.skins.Skin;
	
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import cn.flashk.controls.managers.DefaultStyle;
	import cn.flashk.controls.managers.SkinLoader;
	import cn.flashk.controls.managers.SkinManager;
	import cn.flashk.controls.managers.SourceSkinLinkDefine;
	import cn.flashk.controls.modeStyles.ButtonStyle;
	import cn.flashk.controls.skin.ActionDrawSkin;
	import cn.flashk.controls.skin.ToggleButtonSkin;
	import cn.flashk.controls.skin.sourceSkin.ToggleButtonSourceSkin;
	import cn.flashk.controls.support.ColorConversion;
	import cn.flashk.controls.support.UIComponent;

	/**
	 * 当selected的值因为用户操作或者程序设置更改时触发
	 * @eventType flash.events.Event.CHANGE
	 **/
	[Event(name="change",type="flash.events.Event")]
	/**
	 * ToggleButton 组件定义切换按钮。单击该按钮会在弹起状态和按下状态之间进行切换。如果在按钮处于弹起状态时单击该按钮，则它会切换到按下状态。必须再次单击该按钮才可将其切换回弹起状态。
	 *
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 *
	 * @see cn.flashk.controls.support.UIComponent
	 *
	 * @author flashk
	 */
	public class ToggleButton extends UIComponent{
		protected var _selected:Boolean;
		protected var txt:TextField;
		protected var tf:TextFormat;
		protected var iconWid:Number = 12;
		protected var iconHei:Number = 12;

		public function ToggleButton(skinID:String = ""){
			super(skinID);
			txt = new TextField();
			txt.autoSize = "left"
			this.addChild(txt);
			
			if(_skinID!=""||_skinID=="null"){
				iconWid = Skin.librarys.getSkin(_skinID, "up").width;
				iconHei = Skin.librarys.getSkin(_skinID, "up").height;
				tf = Skin.librarys.getTextFormat(_skinID, "upTxt");
				setSize(iconWid, iconHei);
			}
			if(tf==null){
				tf = new TextFormat();
				tf.align = TextFormatAlign.CENTER;
				tf.size = DefaultStyle.fontSize;
				tf.color = ColorConversion.transformWebColor(styleSet[ButtonStyle.TEXT_COLOR]);
				tf.font = DefaultStyle.font;
			}
			txt.selectable = false;
			txt.mouseEnabled = false;
			txt.setTextFormat(tf);
			txt.blendMode = BlendMode.LAYER;
			this.addEventListener(MouseEvent.CLICK, switchSelect);
		}

		public function set label(value:String):void {
			txt.text = value;
			txt.setTextFormat(tf);
			setSize(_compoWidth,_compoHeight);
		}
		
		/**
		 * 设置/获取按钮是否处于选中状态
		 */
		public function set selected(value:Boolean):void {
			_selected = value;
			skin.updateToggleView(_selected);
			this.dispatchEvent(new Event(Event.CHANGE));
		}

		public function get selected():Boolean {
			return _selected;
		}

		protected function switchSelect(event:MouseEvent):void {
			selected = !_selected;
		}

		override public function setDefaultSkin():void {
			setSkin(ToggleButtonSkin);
		}

		override public function setSourceSkin():void {
			setSkin(SkinLoader.getClassFromSkinFile(SourceSkinLinkDefine.BUTTON));
		}

		override public function setSkin(Skin:Class):void {
			if (SkinManager.isUseDefaultSkin == true){
				skin = new Skin();
				ActionDrawSkin(skin).init(this, styleSet);
			} else {
				var sous:ToggleButtonSourceSkin = new ToggleButtonSourceSkin();
				sous.init(this, styleSet, Skin);
				skin = sous;
			}
		}
		
		override public function setSize(newWidth:Number, newHeight:Number):void {
			super.setSize(newWidth, newHeight);
			txt.width = _compoWidth - 16;
			txt.x = iconWid + 5;
			txt.y = int((_compoHeight - txt.textHeight) / 2);
			//txt.y = txt.y + DefaultStyle.fontExcursion;
		}
		
		public function textFormat(value:TextFormat):void{
			tf = value;
			txt.setTextFormat(tf);
			setSize(_compoWidth,_compoHeight);
		}
		
	}
}